<template>
  <div class="xtx-goods-page" v-if="goodsList.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread >
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodsList.categories[1].id}`">{{
          goodsList.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goodsList.categories[0].id}`">{{
          goodsList.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goodsList.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
            <GoodsImage :imageList="goodsList.mainPictures" class="img"/>
             <GoodsSales :goodsList="goodsList"/>
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodsName :goodsList="goodsList"/>
          <GoodsSku :goods="goodsList" @change="skuChange"/>
          <XtxNumbox  v-model="number" />
          <XtxButton type="primary" style="margin-top:20px;" @click="add">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs/>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
         <GoodsHot :type='1'/>
         <GoodsHot :type='2'/>
         <GoodsHot :type='3'/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/goods'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image'
import GoodsName from './components/goods-name'
import GoodsSales from './components/goods-sales'
import GoodsSku from './Sku'
import GoodsTabs from './components/goods-tabs'
import GoodsHot from './components/goods-hot'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsName,
    GoodsSales,
    GoodsSku,
    GoodsTabs,
    GoodsHot
  },
  setup () {
    const goodsList = ref({})
    const route = useRoute()
    async function getGoodsList () {
      const res = await findGoods(route.params.id)
      console.log('面包屑导航', res)
      goodsList.value = res.data.result
    }
    onMounted(() => {
      getGoodsList()
    })
    const currSku = ref({})
    function skuChange (sku) {
      // 无效sku 返给我们一个空对象
      // 有效sku 完整的sku对象信息
      console.log(sku)
      currSku.value = sku
    }
    const number = ref(1)
    const store = useStore()
    // 点击加入购物车
    function add () {
      if (!currSku.value.skuId) {
        return false
      }
      // 组合一下sku组件给到的有效sku对象 + goods对象
      const good = {
        id: goodsList.value.id,
        name: goodsList.value.name,
        picture: goodsList.value.mainPictures[0],
        skuId: currSku.value.skuId,
        price: currSku.value.price,
        nowPrice: currSku.value.price,
        attrsText: currSku.value.specsText,
        stock: currSku.value.inventory,
        selected: true,
        isEffective: true,
        count: number.value
      }
      store.dispatch('cart/asyncInsertCart', good)
    }
    return {
      goodsList,
      skuChange,
      number,
      add
    }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
